<?php

namespace Admin\Controller;

use Think\Controller;
use Think\Upload;

class MyUploadController extends Controller
{

    public function upload()
    {
        $upload = new Upload();// 实例化上传类
        $upload->exts = ['png', 'jpg', 'jpeg', 'gif', 'bmp','pdf'];
        $upload->rootPath = $this->createDir() . DIRECTORY_SEPARATOR;
        $upload->subName = '';
        $info = $upload->upload();
        $url = [];
        if ($info) {
            $image = new \Think\Image();
            foreach ($info as $k => $v) {
                if($info[$k]['ext'] != 'pdf'){
                    if ($info[$k]['savename']) {
                        $url[$k] = ltrim($this->createDir() . $info[$k]['savename'], '.');
//                    $thumb_file = './Uploads/VideoImg/' . $url[$k]['savename'];
//                    $savePath = './Uploads/VideoImg/' . 'mini' . $url[$k]['savename'];
                        $image->open(APP_PATH.$url[$k])->thumb(1000, 800, \Think\Image::IMAGE_THUMB_SCALE)->save(APP_PATH.$url[$k]);
//                    @unlink($thumb_file); //上传生成缩略图以后删除源文件
                    }
                }else{
                    $url[$k] = ltrim($this->createDir() . $info[$k]['savename'], '.');
                }
            }
        } else {
            return false;
        }
        return $url;
    }

    /**
     * [createDir description]
     * @return [type] [description]
     */
    public function createDir()
    {

        $upload_dir = './Public/Uploads/' . date('Y/m/');

        if (is_dir($upload_dir) || mkdir($upload_dir, 0777, true)) {
            return $upload_dir;
        }

    }
}

  